package com.starbaba.module.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.util.StringUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.model.utils.JSONUtils;
import com.starbaba.module.model.ApiModelItem;
import com.starbaba.module.model.ApiModelItemArray;
import com.starbaba.module.model.LoanApiModel;
import com.starbaba.module.service.ApiModuleService;
import com.starbaba.tools.sequence.Sequence;

@Component
@SuppressWarnings({"rawtypes","deprecation"})
public class ApiModuleServiceImpl implements ApiModuleService{

    static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
    static BaseDao testdao = new BaseDao(Constant.POOL_PUSH_RW);

    public List getDataList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap) {

        String modelName = (String) paramsMap.get("modelName");
        String modelType = (String) paramsMap.get("modelType");
        StringBuffer sf = new StringBuffer();
        StringBuffer tt = new StringBuffer();
        sf.append("SELECT * FROM loan_api_model mo WHERE 1=1 ");

        if (modelName != null && "".equals(modelName)) {
            tt.append("AND mo.model_name LIKE '%" + modelName + "' ");
        }
        if (modelType != null && "".equals(modelType)) {
            tt.append("AND mo.model_type = " + modelType);
        }
        
        sf.append(tt.toString());
        sf.append(" ORDER BY mo.ctime DESC " + "LIMIT " + iDisplayStart + "," + iDisplayLength);
        List<LoanApiModel> list = null;
        try {
            list = dao.list(sf.toString(), LoanApiModel.class);

            String countHql = "select count(*) FROM loan_api_model mo WHERE 1=1 " + tt.toString();
            int count = dao.getInt(countHql, new Object[] {});

            pageResponse.setITotalRecords(count);
            pageResponse.setITotalDisplayRecords(count);
        } catch (Exception e) {

        }
        return list;
    }
    
    public void updateContext(Map<String, Object> paramsMap){
    	String id = (String) paramsMap.get("id");
    	String modelName = (String) paramsMap.get("modelName");
    	String modelRemark = (String) paramsMap.get("modelRemark");
    	String updateSql = "";
    	if(modelName != null){
    		updateSql = "UPDATE loan_api_model SET model_name = '"+modelName+"' WHERE id = ?";
    	}else if(modelRemark!=null){
    		updateSql = "UPDATE loan_api_model SET model_remark = '"+modelRemark+"' WHERE id = ?";
    	}
    	try {
			dao.update(updateSql,new Object[] {id});
		} catch (AppException e) {
			e.printStackTrace();
		}
    }
    
    public LoanApiModel getModelData(Long id) throws AppException{
    	String selectSql = "SELECT * FROM loan_api_model mo WHERE mo.id = ?";
    	LoanApiModel model = dao.get(selectSql, LoanApiModel.class, id);
    	return model;
    }
    
    public void updateModel(Map<String, Object> paramsMap) throws AppException{
    	String updateSql = "UPDATE loan_api_model SET model_name = ?,model_remark = ?,link = ?,model_type = ?,properties = ?,uptime = ? WHERE id = ?";
    	String id = (String) paramsMap.get("id"); 
        String modelName = (String) paramsMap.get("model_name");
        String modelRemark = (String) paramsMap.get("model_remark");
        Integer modelType = Integer.valueOf((String) paramsMap.get("model_type"));
        String link = (String) paramsMap.get("link");
        
        String indexTxt = (String) paramsMap.get("index");
        List<ApiModelItem> propList = new ArrayList<ApiModelItem>();
        String propItemsJson = "";
        if (!StringUtils.isEmpty(indexTxt)) {
        	Integer index = Integer.parseInt(indexTxt);
        	//将同一个所属大项的每一小项归为一个小数组，再把每一个不同大项归为一个大数组
        	for (int i = 0; i < index; i++) {
        		ApiModelItem apiModelItem = new ApiModelItem();
        		String name = (String) paramsMap.get("name" + i);  //title
        		String content = (String) paramsMap.get("content" + i);  //key名称
        		String type = (String) paramsMap.get("type" + i);  
        		String parentObj = (String) paramsMap.get("parentObj"+i); //所属大项   
        		String reqInput = (String) paramsMap.get("reqInput"+i); //是否必填
        		String remark = (String) paramsMap.get("remark" + i);
        		apiModelItem.setKeyName(content);
        		apiModelItem.setModelType(type);
        		apiModelItem.setParentObj(parentObj);
        		apiModelItem.setRemark(remark);
        		apiModelItem.setReqInput(reqInput);
        		apiModelItem.setTitle(name);
        		propList.add(apiModelItem);
        	}
        	propItemsJson = JSONUtils.toJsonString(propList).toString();
        }
        dao.update(updateSql,new Object[] {modelName,modelRemark,link,modelType,propItemsJson,new Date(),id});
    }

}











